/**
 * 
 * 
 */
(function() { 
    /** 
     * @name _Log
     * @namespace _Log
     *
     * Beautify logs 
     */
    var Log = { 
	/** 
	 * Errors
	 * @member
	 */
	e: console.error.bind(console, "%cCheckChrome", "color:#c00"), 
	/** 
	 * Warnings
	 * @member
	 */
	w: console.warn.bind(console, "%cCheckChrome", "color:#cc0"), 
	/**
	 * Information
	 * @member
	 */
	i: console.log.bind(console, "%cCheckChrome", "color:#00c"),
	/** 
	 * Saved debug
	 * @member
	 */
	s: console.log.bind(console, "%cCheckChrome", "color:#c0c"),
	/** 
	 * Turn debugging on
	 * @member
	 */
	debugOn : function() { 
	    /** @ignore */
	    Log.d=s;
	},
	/** 
	 * Turn debugging off
	 * @member
	 */
	debugOff : function() { 
	    /** @ignore */
	    Log.d=function() {};
	}
	// i: function () {}
    };
    Log.debugOff();
    /** 
     * Check for the presence of the ChromeCast extension  
     *
     * @name ChromeChecker
     * @public 
     * @class ChromeChecker
     * @constructor 
     */
    var ChromeChecker = function(id) { 
	/**
	 * The cast extension ID
	 * @member
	 */
	var CAST_EXTENSION_ID = "boadgeojelhgndaghljhdicfkmllpafd";  
	/** 
	 * our ID 
	 * @lends ChromeChecker.prototype
	 */
	this.mId = id;
	/** 
	 * Run the check
	 *
	 * @param callback Call-back 
	 * @methodOf ChromeChecker.prototype
	 */
	this.check = function(callback) { 
	    Log.i("Checking for ChromeCast extension");
	    // Generally a bad policy to check for a specific browser
	    // - we should instead check for features i.e., presence
	    // of ChromeCast extension.  However, I think this is
	    // pretty safe - for now - since the ChromeCast extension
	    // is unlikely to hit any other browser than Chromium any
	    // time soon.
	    //
	    // Since version 15, Opera has used the same back-end as
	    // Chrome, so we need to rule that out too.
	    // 
	    // var isOpera            = (!!window.opera ||
	    // 		navigator.userAgent.indexOf(' OPR/') >= 0);
	    // var isChrome           = !!window.chrome && !isOpera;
	    // var img       = new Image;
	    // img.onload    = this.initializeCastControl.bind(this);
	    // img.onerror   = this.disableUs.bind(this);
	    // img.src       = ("chrome-extension://" + castExtensionID +
	    //                  "/data/icon16.png");
	    try {
		var xhr       = new XMLHttpRequest;
		var url       = ("chrome-extension://" + CAST_EXTENSION_ID +
		                 "/cast_sender.js");
		// var url       = "about:plugins";
		xhr.onload    = this.onSuccess.bind(this);
		xhr.onerror   = this.onError.bind(this);
		xhr.ontimeout = this.onError.bind(this);
		xhr.onabort   = this.onError.bind(this);
		xhr.timeout   = 4000; // 4 seconds
		xhr.open("GET", url, true);
		Log.d("Sending XHR request");
		xhr.send();
	    } catch (e) {
		Log.w("Failed to do XML request over http: " + e);
		this.onError();
	    }
	}
	/** 
	 * Insert into a element
	 * 
	 * @param e Element id
	 *
	 * @methodOf ChromeChecker.prototype 
	 */
	this.insertId = function(e) {
	    var h = e.getElementsByTagName("a")[1];
	    h.href = "https://chrome.google.com/webstore/detail/google-cast/" 
		+  CAST_EXTENSION_ID;
	}
	/** 
	 * Write a message 
	 * 
	 * @param e Element to write to 
	 *
	 * @methodOf ChromeChecker.prototype 
	 */
	this.writeMsg = function(e) { 
	    var u = "https://chrome.google.com";
	    var t = "<table><tr><td>"
		+ "<div class='head'>Cast DR</div>"
		+ "This app only works with <a href='" + u + "'>Chrome</a> "
		+ "with the ChromeCast " 
		+ "<a href='"+u+"/detail/google-cast/"+CAST_EXTENSION_ID+"'>" 
		+ "extension</a> installed.<br>"
		+ "<span class='sub'>UI string: "+navigator.userAgent+"</span>"
		+ "</td></tr></table>";
	    e.innerHTML = t;
	}
	/** 
	 * Call-back on error
	 *
	 * @methodOf ChromeChecker.prototype 
	 */
	this.onError = function() { 
	    Log.w("No extension found!");
	    var e = document.getElementById(this.mId);
	    this.writeMsg(e);
	    e.style.display = "block";
	    window.stop();
	    // setTimeout(window.stop.bind(window), 100);
	}
	/** 
	 * Call-back on success
	 *
	 * @methodOf ChromeChecker.prototype 
	 */
	this.onSuccess = function() { 
	    Log.i("Found the extension!");
	}
	// Run check 
	this.check();
    };
    // Make available outside 
    window.ChromeChecker = ChromeChecker;
})();
/*
 * EOF
 */
